package com.tanhua.dubbo.api.impl.mongo;

import com.tanhua.domain.db.Question;
import com.tanhua.domain.mongo.*;
import com.tanhua.dubbo.api.mongo.SoulApi;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class SoulApiImpl implements SoulApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Questionnaire> testSoul() {
        List<Questionnaire> questionnaires = mongoTemplate.findAll(Questionnaire.class);
        return questionnaires;
    }

    @Override
    public List<Topic> findAllTopic(Integer qid) {
        return mongoTemplate.find(Query.query(Criteria.where("qid").is(qid)),Topic.class);
    }

    @Override
    public List<Options> findAllOptions(Integer topId) {
        Query query = new Query((Criteria.where("topId").is(topId)));
        return mongoTemplate.find(query,Options.class);
    }

    @Override
    public Double findScore(Integer questionId, String optionId) {
        Query query = new Query(Criteria.where("topId").is(questionId).and("keyId").is(optionId));
        Options one = mongoTemplate.findOne(query, Options.class);
        return one.getScore();
    }

    @Override
    public void save(Conclusion conclusion) {
        Query query = new Query(Criteria.where("userId").is(conclusion.getUserId()).and("level").is(conclusion.getLevel()));
        Conclusion one = mongoTemplate.findOne(query, Conclusion.class);
        if (one==null) {
            mongoTemplate.save(conclusion);
        }else {
            Update update = new Update();
            update.set("type",conclusion.getType());
            update.set("reportId",conclusion.getReportId());
            update.set("score",conclusion.getScore());
            mongoTemplate.updateFirst(query,update,Conclusion.class);
        }
    }

    @Override
    public List<Conclusion> findConclusion(Long userId) {
        return mongoTemplate.find(Query.query(Criteria.where("userId").is(userId)),Conclusion.class);
    }

    @Override
    public Integer findQid(Integer questionId) {
        Query query = new Query(Criteria.where("topId").is(questionId));
        Topic topic = mongoTemplate.findOne(query, Topic.class);
        return topic.getQid();
    }

    @Override
    public String findLevel(Integer qid) {
        Query query = new Query(Criteria.where("qid").is(qid));
        Questionnaire questionnaire = mongoTemplate.findOne(query, Questionnaire.class);
        return questionnaire.getLevel();
    }

    @Override
    public Set<Long> findAllType(Long userId,Long reportId) {
        //Query query = new Query(Criteria.where("reportId").is(reportId).and("userId").is(userId));
        //List<Conclusion> conclusions = mongoTemplate.find(query,Conclusion.class);
        List<Conclusion> conclusions = mongoTemplate.findAll(Conclusion.class);
        Set<Long> set = new HashSet<>();
        if(!CollectionUtils.isEmpty(conclusions)){
            for (Conclusion conclusion : conclusions) {
                if(conclusion.getReportId().equals(reportId.toString())&&!conclusion.getReportId().equals("")) {
                    set.add(conclusion.getUserId());
                }
            }
        }

        return set;
    }

    @Override
    public PersonalityType findReport(Long reportId) {
        Query query = new Query(Criteria.where("type").is(reportId));
        return mongoTemplate.findOne(query,PersonalityType.class);
    }

    @Override
    public void update(Conclusion conclusions) {
        Query query = new Query(Criteria.where("userId").is(conclusions.getUserId()).and("level").is(conclusions.getLevel()));
        Conclusion one = mongoTemplate.findOne(query, Conclusion.class);
        if(one==null){
            if(conclusions.getLevel()!=null) {
                mongoTemplate.save(conclusions);
            }
        }
    }

}
